<template>
  <div>
    <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      append-to-body
      :visible.sync="visible"
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="120px"
        v-loading="loading"
      >
        <el-form-item label="活动场次id" prop="promotionSessionId">
          <el-input v-model="sessionId" placeholder="活动场次id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品id" prop="skuId">
          <el-input v-model="dataForm.skuId" placeholder="商品id" style="width: 200px; margin-right: 20px;"></el-input>
          <el-button type="primary" icon="el-icon-edit" @click="addSkuButton"></el-button>
        </el-form-item>
        <el-form-item label="秒杀价格" prop="seckillPrice">
          <el-input-number v-model="dataForm.seckillPrice" :min="0" :precision="2" :step="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="秒杀总量" prop="seckillCount">
          <el-input-number v-model="dataForm.seckillCount" :min="1" label="秒杀总量"></el-input-number>
        </el-form-item>
        <el-form-item label="每人限购数量" prop="seckillLimit">
          <el-input-number v-model="dataForm.seckillLimit" :min="1" label="每人限购数量"></el-input-number>
        </el-form-item>
        <el-form-item label="排序" prop="seckillSort">
          <el-input v-model="dataForm.seckillSort" placeholder="排序"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="添加商品"
      :close-on-click-modal="false"
      append-to-body
      :visible.sync="addSkuVisible">
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
      >
        <el-table-column header-align="center" align="center" width="50">
          <template slot-scope="scope">
            <el-radio v-model="radioSkuId" :label="scope.row.skuId">{{''}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="skuName" header-align="center" align="center" label="名称"></el-table-column>
        <el-table-column prop="skuDefaultImg" header-align="center" align="center" label="默认图片">
          <template slot-scope="scope">
            <img :src="scope.row.skuDefaultImg" style="width:80px;height:80px;" />
          </template>
        </el-table-column>
        <el-table-column prop="price" header-align="center" align="center" label="价格"></el-table-column>
        <el-table-column prop="saleCount" header-align="center" align="center" label="销量"></el-table-column>
      </el-table>

      <el-pagination
        style="margin-top: 10px"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[4, 8, 12, 16]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addSkuVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddSku">关 联</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { info, add, update } from '@/api/coupon/seckill-sku-relation'
import { getSkuInfoNotSkuIdList } from '@/api/goods/sku-info'

export default {
  data() {
    return {
      radioSkuId: null,
      addSkuVisible: false,
      dataListLoading: false,
      selectSku: [],
      visible: false,
      loading: false,
      pageIndex: 1,
      pageSize: 4,
      totalPage: 0,
      key: null,
      dataList: [],
      dataForm: {
        id: 0,
        promotionId: "",
        promotionSessionId: "",
        skuId: "",
        seckillPrice: "",
        seckillCount: "",
        seckillLimit: 1,
        seckillSort: 0
      },
      dataRule: {
        sessionId: [
          { required: true, message: "活动场次id不能为空", trigger: "blur" }
        ],
        skuId: [{ required: true, message: "商品id不能为空", trigger: "blur" }],
        seckillPrice: [
          { required: true, message: "秒杀价格不能为空", trigger: "blur" }
        ],
        seckillCount: [
          { required: true, message: "秒杀总量不能为空", trigger: "blur" }
        ],
        seckillLimit: [
          { required: true, message: "每人限购数量不能为空", trigger: "blur" }
        ],
        seckillSort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  props: {
    sessionId: {
      type: String,
      default: 0
    }
  },
  methods: {
    init(id, skuIds) {
      this.key = skuIds
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true
          info(this.dataForm.id)
          .then(({ data }) => {
            this.dataForm.promotionId = data.promotionId;
            this.dataForm.promotionSessionId =
              data.promotionSessionId;
            this.dataForm.skuId = data.skuId;
            this.dataForm.seckillPrice = data.seckillPrice;
            this.dataForm.seckillCount = data.seckillCount;
            this.dataForm.seckillLimit = data.seckillLimit;
            this.dataForm.seckillSort = data.seckillSort;
            this.loading = false
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      console.log("提交")
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          const query = {
            id: this.dataForm.id || undefined,
            promotionId: this.dataForm.promotionId,
            promotionSessionId: this.sessionId,
            skuId: this.dataForm.skuId,
            seckillPrice: this.dataForm.seckillPrice,
            seckillCount: this.dataForm.seckillCount,
            seckillLimit: this.dataForm.seckillLimit,
            seckillSort: this.dataForm.seckillSort
          }
          if(!this.dataForm.id) {
            add(query)
            .then(() => {
              this.$message({
                message: "操作成功",
                type: "success"
              });
              this.visible = false;
              this.$emit("refreshDataList");
            });
          } else {
            update(query)
              .then(() => {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.visible = false;
                this.$emit("refreshDataList");
              });
          }
        }
      });
    },
    addSkuButton() {
      this.addSkuVisible = true
      this.dataListLoading = true
      getSkuInfoNotSkuIdList(
        {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.key
        })
        .then(({data}) => {
          console.log(data)
          this.dataList = data.list;
          this.totalPage = data.totalCount;
          this.dataListLoading = false
        });
    },
    selectionChangeHandle(val) {
      this.selectSku = val
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.addSkuButton();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.addSkuButton();
    },
    submitAddSku() {
      this.dataForm.skuId = this.radioSkuId
      this.addSkuVisible = false
    }
  }
};
</script>
